package com.awspaas.user.apps.processforsap.pluginImpl;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GetGirdFieldValues  extends AbstExpression {
    public GetGirdFieldValues(final ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    @Override
    public String execute(String expression) {
        ExpressionContext result = getExpressionContext();
        ProcessInstance processInstance = result.getProcessInstance();
        //获取表名
        String boName = getParameter(expression, 1);
        //获取数据字段名称
        String filedName = getParameter(expression, 2);
        Set<String> resultData = new HashSet<>();
        if (processInstance!=null){
            List<BO> resultList = SDK.getBOAPI().query(boName).bindId(processInstance.getId()).list();
            for (BO bo1:resultList){
                String value= bo1.getString(filedName);
                resultData.add(value);
            }
        }
        if (resultData.size() ==0){
            return "空";
        }else {
            return StringUtils.join(resultData,",");
        }
    }
}
